/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package facecat;
import javafx.scene.canvas.*;
import facecat.topin.core.*;
import facecat.topin.xml.*;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseButton;

/**
 *
 * @author Administrator
 */
public class FCUIView extends Canvas {
    private FxHost m_host = null;
    
    /*
    * 获取设备库
    */
    public FxHost getHost (){
        return m_host;
    }
    
    private FCNative m_native = null;

    /*
    * 获取方法库
    */
    public FCNative getNative ()
    {
        return m_native;
    }

    private FxPaint m_paint = null;

    /*
    * 获取绘图库
    */
    public FxPaint getPaint ()
    {
        return m_paint;
    }
    
    /*
    * 主界面
    */
    public UIXmlEx m_mainFrame;
    
    /*
    * 初始化
    */
    public void onLoad (Scene scene){
        m_host = new FxHost();
        m_native = new FCNative();
        m_paint = new FxPaint();
        m_native.setHost(m_host);
        m_native.setPaint(m_paint);
        m_host.setNative(m_native);
        m_host.setView(this);
        m_native.setSize(new FCSize((int)getWidth(), (int)getHeight()));
        m_native.setAllowScaleSize(true);
        
        m_mainFrame = new MainFrame();
        m_mainFrame.setNative(m_native);
        String content = "";
        RefObject<String> refContent = new RefObject<String>(content);
        String dir = DataCenter.getAppPath() + DataCenter.m_seperator + "example" + DataCenter.m_seperator + "MainFrame.xml";
        FCFile.read(dir, refContent);
        content = refContent.argvalue;
        m_mainFrame.loadXml(content, null);
        m_mainFrame.resetScaleSize(new FCSize((int)getWidth(), (int)getHeight()));
        m_native.update();
       m_native.invalidate();
       
        setOnMouseDragged(e -> {  
            double x = e.getX();  
            double y = e.getY(); 
            m_host.setTouchPoint(new FCPoint((int)x, (int)y));
            FCTouchInfo newTouchInfo = new FCTouchInfo();
            newTouchInfo.m_firstPoint = m_native.getTouchPoint();
            newTouchInfo.m_secondPoint = m_native.getTouchPoint();
            if(e.getButton() == MouseButton.PRIMARY){
                newTouchInfo.m_firstTouch = true;
                newTouchInfo.m_buttons = 1;
            }else if(e.getButton() == MouseButton.SECONDARY){
                newTouchInfo.m_secondTouch = true;
                newTouchInfo.m_buttons = 2;
            }
            //m_lastButton = e.getButton();
            newTouchInfo.m_clicks = e.getClickCount();
            m_native.onTouchMove(newTouchInfo);
        });   
        setOnMouseMoved(e -> {  
            double x = e.getX();  
            double y = e.getY(); 
            m_host.setTouchPoint(new FCPoint((int)x, (int)y));
            FCTouchInfo newTouchInfo = new FCTouchInfo();
            newTouchInfo.m_firstPoint = m_native.getTouchPoint();
            newTouchInfo.m_secondPoint = m_native.getTouchPoint();
            m_native.onTouchMove(newTouchInfo);
        });  
        setOnMousePressed(e -> {  
            double x = e.getX();  
            double y = e.getY();  
            m_host.setTouchPoint(new FCPoint((int)x, (int)y));
            FCTouchInfo newTouchInfo = new FCTouchInfo();
            newTouchInfo.m_firstPoint = m_native.getTouchPoint();
            newTouchInfo.m_secondPoint = m_native.getTouchPoint();
            if(e.getButton() == MouseButton.PRIMARY){
                newTouchInfo.m_firstTouch = true;
                newTouchInfo.m_buttons = 1;
            }else{
                newTouchInfo.m_secondTouch = true;
                newTouchInfo.m_buttons = 2;
            }
            newTouchInfo.m_clicks = e.getClickCount();
            m_native.onTouchBegin(newTouchInfo);
        });  
        setOnMouseReleased(e -> {  
            double x = e.getX();  
            double y = e.getY();  
            m_host.setTouchPoint(new FCPoint((int)x, (int)y));
            FCTouchInfo newTouchInfo = new FCTouchInfo();
            newTouchInfo.m_firstPoint = m_native.getTouchPoint();
            newTouchInfo.m_secondPoint = m_native.getTouchPoint();
            if(e.getButton() == MouseButton.PRIMARY){
                newTouchInfo.m_firstTouch = true;
                newTouchInfo.m_buttons = 1;
            }else{
                newTouchInfo.m_secondTouch = true;
                newTouchInfo.m_buttons = 2;
            }
            newTouchInfo.m_clicks = e.getClickCount();
            m_native.onTouchEnd(newTouchInfo);
        });  
        setOnScroll(e -> {  
            double x = e.getX();  
            double y = e.getY();  
            m_host.setTouchPoint(new FCPoint((int)x, (int)y));
            FCTouchInfo newTouchInfo = new FCTouchInfo();
            newTouchInfo.m_firstPoint = m_native.getTouchPoint();
            newTouchInfo.m_secondPoint = m_native.getTouchPoint();
            newTouchInfo.m_delta = (int)e.getDeltaY();
            m_native.onTouchWheel(newTouchInfo);
        });  
        scene.setOnKeyPressed(e -> {
            char keyCode = e.getCharacter().charAt(0);
            m_host.m_isAlt = e.isAltDown();
            m_host.m_isControl = e.isControlDown();
            m_host.m_isShift = e.isShiftDown();
            KeyCode key = e.getCode();
            if(key == KeyCode.BACK_SPACE){
                keyCode = 8;
            }else if(key == KeyCode.UP){
                keyCode = 38;
            }else if(key == KeyCode.DOWN){
                keyCode = 40;
            }else if(key == KeyCode.LEFT){
                keyCode = 37;
            }else if(key == KeyCode.RIGHT){
                keyCode = 39;
            }
            if(keyCode == 8 || keyCode == 37 || keyCode == 38 || keyCode == 39 || keyCode == 40 || m_host.m_isControl){
                m_native.onKeyDown(keyCode);
                //m_native.onChar(e.getKeyChar());
            }
        });  
        scene.setOnKeyReleased(e -> {  
           char keyCode = e.getCharacter().charAt(0);
            if(keyCode != 0){
                m_native.onKeyUp(keyCode);
            }else{
                m_native.onKeyUp(e.getCharacter().charAt(0));
            }
        }); 
        scene.setOnKeyTyped(e -> {  
            char keyCode = e.getCharacter().charAt(0);
            m_host.m_isAlt = e.isAltDown();
            m_host.m_isControl = e.isControlDown();
            m_host.m_isShift = e.isShiftDown();
            KeyCode key = e.getCode();
            if(key != KeyCode.BACK_SPACE && key != KeyCode.UP && key != KeyCode.DOWN && key != KeyCode.LEFT && key != KeyCode.RIGHT){
                m_native.onChar(keyCode);
            }
        });  
         scene.widthProperty().addListener((observable, oldValue, newValue) -> {  
            draw();
        });  
  
        scene.heightProperty().addListener((observable, oldValue, newValue) -> { 
            draw();
        });  
    }
    
    public FCSize m_lastSize = new FCSize();
    
    public GraphicsContext m_context;
    
    /*
    * 绘图方法
    */
    public void draw(){
        if(m_host != null){
            FCSize nowSize = new FCSize((int)getWidth(), (int)getHeight());
            if(nowSize.cx != m_lastSize.cx || nowSize.cy != m_lastSize.cy){
                m_mainFrame.resetScaleSize(nowSize);
            }
            if(m_context == null){
                m_context = getGraphicsContext2D();
            }
            m_host.onPaint(m_context);
            m_lastSize = nowSize;
        }
    }
}
